/*
 * Written by Dustin Striplin
 */

package TableModels;

import java.util.ArrayList;

/*
 * This class is used by the table. This class defines the format of the table.
 */


import Model.Constants;
import Model.Manuscript;
import Model.Review;

public class AuthorTemplate extends MasterTable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] columnNames = {"Title", "Approval"};
	private ArrayList<ArrayList<Object>> data;  //2D ArrayList containing the individual cells in the table
	private ArrayList<Manuscript> manuscripts;		//saved to store data that is hidden from the table.
	
	
	
	
	public AuthorTemplate(final ArrayList<Manuscript> the_data) {
		manuscripts = new ArrayList<Manuscript> (the_data);  //make a deep copy of the manuscript list
		data = new ArrayList<ArrayList<Object>>();
		storeData(the_data);
	}

	/**
	 * Adds a row to the table
	 * @param script the manuscript to add
	 */
	public void addRow(Manuscript script) {
		ArrayList<Object> newRow = new ArrayList<Object>();
		newRow.add(script.getTitle());
		newRow.add(Constants.convertItoConst(script.getApproval()));
		data.add(newRow);			//update table data
		manuscripts.add(script);	//update manuscript list
	}
	
	@Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    /**
     * Returns the approval status of the paper
     * @param row the row number
     * @return the approval status
     */
    public int getApproval(int row) {
    	
    	return manuscripts.get(row).getApproval();
    }
    
    /**
     * Deletes a manuscript & updates TableModel's data
     * @param row the row number
     */
    public void deleteManuscript(int row) {
    	data.remove(row);
    	manuscripts.remove(row);
    }
    
    /**
     * @param row
     * @return the requested manuscript
     */
    public Manuscript getManuscript(int row) {
    	
    	return new Manuscript(manuscripts.get(row));
    }
    /**
     * returns a link to the manuscript.
     * @param row the desired row.
     * @return the link
     */
    public String getManuscriptLink(int row) {
    	return manuscripts.get(row).getLink();
    }
    
    /**
     * Returns an ArrayList containing the reviews
     * @param row the row number
     */
    public ArrayList<Review> getReviews(int row) {
    	return manuscripts.get(row).getReviews();
    }
    
	/**
	 * Returns the number of columns
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}
	
	/**
	 * 	Returns the most specific superclass for all the cell values in the column.
	 */
	public Class<?> 	getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}

	
	/**
	 * Returns the number of rows.
	 */
	@Override
	public int getRowCount() {
		return data.size();
	}
	
	
	@Override
	public Object getValueAt(int row, int column) {
		return data.get(row).get(column);
	}
	
	
	@Override
	public void setValueAt(Object aValue, int row, int col) {
		data.get(row).set(col, aValue);
		fireTableCellUpdated(row, col);
	}
	
	/**
	 * updates internal manuscript list, and table elements
	 * 
	 * @param newM the new manuscript
	 * @param row the row to set
	 */
	public void setManuscript(Manuscript newM, int row) {
		manuscripts.set(row, new Manuscript(newM));
		data.set(row, new ArrayList<Object>());
		data.get(row).add(newM.getTitle());
		data.get(row).add(Constants.convertItoConst(newM.getApproval()));
	}
	
	
	/**
	 * Returns true if a column is editable, false otherwise.
	 */
	@Override
	public boolean isCellEditable(int row, int col) {
		return false;
	}
	
	/**
	 * Stores all of the data a program chair would need, I chose program chair 
	 * because it is the most complicated.
	 * @param data the table contents
	 */
	private void storeData(final ArrayList<Manuscript> the_data) {
		int i = 0;
		for(Manuscript m: the_data) {  //Represents a row
			data.add(new ArrayList<Object>());	//create a new ArrayList that represents a single paper
			data.get(i).add(m.getTitle());		//All of the stuff in the for loop represents
												//a column in the row.
			data.get(i).add(Constants.convertItoConst(m.getApproval()));
			i++;
		}
	}												//TODO, I want the reviewers name to be a link to the reviews.
}	